import typeToReducer from 'type-to-reducer'
import {
  GET_USER_INFO,
  PUT_USER_INFO,
  GET_USER_SIGN,
  GET_USER_CHATS,
  GET_USER_SHARE,
  GET_USER_COLLECT,
  DEL_USER_COLLECT,
  GET_USER_HISTORY,
  GET_USER_ORDER,
  POST_USER_COLLECT,
  POST_USER_HISTORY,
  GET_USER_LIKE,
  POST_USER_RELATION
} from './../constants/actionTypes'


const initialState = {
  data: null,
  isLoading: false,
  error: false
}

const initialState2 = {
  data: [],
  isLoading: false,
  error: false
}


//用户信息
export const userInfoReducer = typeToReducer({
  [GET_USER_INFO]: {
    LOADING: () => ({
      ...initialState,
      isLoading: true,
    }),
    SUCCESS: (state, action) => {
      return {
        ...initialState,
        data: action.payload.data,
        meta: action.payload.meta
      }
    },
    ERROR: (state, action) => ({
      ...initialState,
      error: action.payload
    })
  }
}, initialState)


//更新用户信息
export const updateUserReducer = typeToReducer({
  [PUT_USER_INFO]: {
    LOADING: () => ({
      ...initialState,
      isLoading: true,
    }),
    SUCCESS: (state, action) => {
      return {
        ...initialState,
        data: action.payload.data,
      }
    },
    ERROR: (state, action) => ({
      ...initialState,
      error: action.payload
    })
  }
}, initialState)


//备案淘宝客渠道
export const saveRelationReducer = typeToReducer({
  [POST_USER_RELATION]: {
    LOADING: () => ({
      ...initialState,
      isLoading: true,
    }),
    SUCCESS: (state, action) => {
      return {
        ...initialState,
        data: action.payload.data,
      }
    },
    ERROR: (state, action) => ({
      ...initialState,
      error: action.payload
    })
  }
}, initialState)



//更新用户信息
export const userSignReducer = typeToReducer({
  [GET_USER_SIGN]: {
    LOADING: () => ({
      ...initialState,
      isLoading: true,
    }),
    SUCCESS: (state, action) => {
      return {
        ...initialState,
        data: action.payload.data,
      }
    },
    ERROR: (state, action) => ({
      ...initialState,
      error: action.payload
    })
  }
}, initialState)


//用户积分记录
export const userChatsReducer = typeToReducer({
  [GET_USER_CHATS]: {
    LOADING: () => ({
      ...initialState2,
      isLoading: true,
    }),
    SUCCESS: (state, action) => {
      return {
        ...initialState2,
        data: action.payload.data,
        meta: action.payload.meta,
      }
    },
    ERROR: (state, action) => ({
      ...initialState2,
      error: action.payload
    })
  }
}, initialState2)


//推荐的人
export const userShareReducer = typeToReducer({
  [GET_USER_SHARE]: {
    LOADING: () => ({
      ...initialState2,
      isLoading: true,
    }),
    SUCCESS: (state, action) => {
      return {
        ...initialState2,
        data: action.payload.data,
        meta: action.payload.meta,
      }
    },
    ERROR: (state, action) => ({
      ...initialState2,
      error: action.payload
    })
  }
}, initialState2)


//用户收藏
export const userCollectReducer = typeToReducer({
  [GET_USER_COLLECT]: {
    LOADING: () => ({
      ...initialState2,
      isLoading: true,
    }),
    SUCCESS: (state, action) => {
      return {
        ...initialState2,
        data: action.payload.data,
      }
    },
    ERROR: (state, action) => ({
      ...initialState2,
      error: action.payload
    })
  }
}, initialState2)


//删除收藏记录
export const delUserCollectReducer = typeToReducer({
  [DEL_USER_COLLECT]: {
    LOADING: () => ({
      ...initialState,
      isLoading: true,
    }),
    SUCCESS: (state, action) => {
      return {
        ...initialState,
        data: action.payload.data,
      }
    },
    ERROR: (state, action) => ({
      ...initialState,
      error: action.payload
    })
  }
}, initialState)


//用户浏览记录
export const userHistoryReducer = typeToReducer({
  [GET_USER_HISTORY]: {
    LOADING: () => ({
      ...initialState2,
      isLoading: true,
    }),
    SUCCESS: (state, action) => {
      return {
        ...initialState2,
        data: action.payload.data,
      }
    },
    ERROR: (state, action) => ({
      ...initialState2,
      error: action.payload
    })
  }
}, initialState2)


//用户订单记录
export const userOrderReducer = typeToReducer({
  [GET_USER_ORDER]:{
    LOADING: () => ({
    ...initialState2,
    isLoading: true,
  }),
    SUCCESS: (state, action) => {
      return {
        ...initialState2,
        data: action.payload.data,
      }
    },
    ERROR: (state, action) => ({
      ...initialState2,
      error: action.payload
    })
  }
}, initialState2)


//用户上传浏览记录
export const postUserHistoryReducer = typeToReducer({
  [POST_USER_HISTORY]:{
    LOADING: () => ({
      ...initialState,
      isLoading: true,
    }),
    SUCCESS: (state, action) => {
      return {
        ...initialState,
        data: action.payload.data,
      }
    },
    ERROR: (state, action) => ({
      ...initialState,
      error: action.payload
    })
  }
}, initialState)



//用户是否收藏
export const userLikeReducer = typeToReducer({
  [GET_USER_LIKE]:{
    LOADING: () => ({
      ...initialState,
      isLoading: true,
    }),
    SUCCESS: (state, action) => {
      return {
        ...initialState,
        data: action.payload.data,
      }
    },
    ERROR: (state, action) => ({
      ...initialState,
      error: action.payload
    })
  }
}, initialState)



//用户上传收藏
export const postUserCollectReducer = typeToReducer({
  [POST_USER_COLLECT]:{
    LOADING: () => ({
      ...initialState,
      isLoading: true,
    }),
    SUCCESS: (state, action) => {
      return {
        ...initialState,
        data: action.payload.data,
      }
    },
    ERROR: (state, action) => ({
      ...initialState,
      error: action.payload
    })
  }
}, initialState)